<?php
require_once("Xiachufang/entity/Entity.php");
require_once("Xiachufang/util/Logger.php");
require_once("Xiachufang/util/MainConfig.php");
require_once("Xiachufang/util/DbMgr.php");
class EntityCodeGenerator {
    
public static function underlineToCamel($key) {
        $p = strpos($key,'_');
        if ($p===false) {
            return $key;
        }
        $newKey = substr($key,0,$p);
        for(;;) {
            $lastPos = $p+1;
            $p = strpos($key,'_',$lastPos);
            if ($p===false) {
                $newKey .= ucfirst(substr($key,$lastPos,strlen($key)-$lastPos+1));
                break;
            }
            $newKey .= ucfirst(substr($key,$lastPos,$p-$lastPos));
        }
        return $newKey;
    }

    public static function splitCamel($str) {
        $len = strlen($str);
        $list = array();
        $name = '';
        for($i=0;$i<$len;$i++) {
            $c = $str[$i];
            if (ord($c)>=ord('A') && ord($c)<=ord('Z')) {
                if ($name!='') {
                    $list[] = $name;
                }
                $name = strtolower($c);
            }
            else {
                $name .= $c;
            }
        }
        $list[] = $name;
        return $list;
    }

    public static function camelToUnderline($key) {
        $split = $this->splitCamel($key);
        $count = count($split);
        if ($count==1) {
            return $s;
        }
        $newKey = $split[0];
        for($i=1;$i<$count;$i++) {
            $newKey .= "_".$split[$i];
        }
        return $newKey;
    }
    
    public function generator($tableList) {
        if (is_string($tableList)) {
            $tableList = explode(",",$tableList);
        }
        if(is_array($tableList)) {
            foreach ($tableList as $table) {
                $table = trim($table);
                $this->generatorEntity($table);
            }
        }
        else {
            echo "invalid argument";
        }
    }
    
    private function generatorEntity($table) {
        $entityName = ucfirst(self::underlineToCamel($table));
        $className = $entityName."Entity";
        $db = DBMgr::getSlaver($table);
        $rs = $db->query("show columns from $table");
        if (!$rs->isSuccess()) {
            echo "fetch $table column information failed";
            return;
        }
        $content = <<<ENTITY
<?php
require_once("Entity.php");
class $className extends DBEntity {

ENTITY;
        $primaryKey = '';
        $columnList = '';
        $propertyMap = '';
        while($rs->hasNext()) {
            $row = $rs->next();
            $field = $row['Field'];
            $columnList .= '"'.$field.'",';
            $default = $row['Default'];
            $dbType = $row['Type'];
            $type = $this->fixType($dbType);
            $name = self::underlineToCamel($field);
            $propertyMap .= "\t\"$field\"    => \"$name\",\n";
            if ($row['Key']=='PRI') {
                $primaryKey = $field;
            }
            $content .= <<<ENTITY

	/** @var $type,@column $field,@dbType $dbType */
	var \$$name;

ENTITY;
        }
        $content .= <<<ENTITY

	private static \$_columnList = array(
		$columnList
	);

	private static \$_propertyMap = array(
$propertyMap\t);

	public function __construct() {
		//parent::__construct();
    }
    
    public function getTable() {
        return "$table";
    }

	public function getColumnList() {
		return self::\$_columnList;
	}

	protected function getPropertyMap() {
		return self::\$_propertyMap;
	}

    public function getPrimaryKey() {
        return "$primaryKey";
    }

ENTITY;
        
        $content .= <<<ENTITY

}
?>
ENTITY;
        $fp = fopen(dirname(__FILE__)."/".$entityName.".php","wb");
        fprintf($fp,$content);
        fclose($fp);
        echo "generator entity for $table success\n";
    }
    
    private function fixType($type) {
        if ($type=='text') {
            return "string";
        }
        if (preg_match("/char/i",$type)) {
            return "string";
        }
        if (preg_match("/int/",$type)) {
            return "int";
        }
        return "string";
    }
}
?>